package org.grow.cooldown.ScoreModule;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.grow.cooldown.ScoreModule.entity.Score;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

@RestControllerAdvice
public class ScoreExceptionHandler {

    @ExceptionHandler(value = Score.ScoreException.class)
    public Map handler1(Score.ScoreException ex) {
        return new HashMap() {{
            put("message", ex.getMessage());
            put("code", ex.getCode());
        }};
    }

//    @ExceptionHandler(value = LoginIntercepter.LoginExcpt.class)
//    public Map handler2(LoginIntercepter.LoginExcpt ex) {
//        return new HashMap() {{
//            put("message", ex.getMessage());
//            put("code", ex.getCode());
//        }};
//    }

    public static void throwEx(int code ,String message, HttpServletResponse response) {
        response.setStatus(200);
        PrintWriter writer = null;
        try {
             writer = response.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        writer.write(String.format(
                "{ \"code\": %d ,\"message\": \"%s\"}",
                code,message));

        writer.flush();
        writer.close();
        return;
    }
}
